var a,adr,auth,Delay=350,GM
var mp,dis=[1,1,1,1,1,1,1,1,1,1],b,i,j
var op={weekday:"short",year:"numeric",month:"numeric",day:"2-digit",hour:"2-digit",minute:"2-digit",second:"2-digit"}

var Rel_chg_a,IO_chg_a,Log_chg_a
var bga="background-image: linear-gradient(to bottom, #adadad, #a6a6a6 50%, #999999 50%, #8e7b7c);background-image: -webkit-linear-gradient(top, #adadad 0%, #a6a6a6 50%, #999999 50%, #8e7b7c);"
var bge="background-image: linear-gradient(to bottom, #45b436, #3ca933 50%, #3e952d 50%, #337c1d);background-image: -webkit-linear-gradient(top, #45b436 0%, #3ca933 50%, #3e952d 50%, #337c1d);"
var bgs="background-color:"
var tb="<table cellspacing=7 border=0 style='font-size:8.5pt;background:#bbb;color:#FFF;width:580px'><tr height=30>"
var tb1="<table cellspacing=7 border=0 style='background:#bbb;color:#FFF;width:300px'>"
var bg_e="#059105'>"
var bg_a="#5A4848'>"
var bg_ee="#837373;border:1px inset #ccc'>"
var bg_ea="#6F9576;border:1px inset #ccc'>"
var newP = document.createElement('p')
var ts,tt
var i_div=100
var cos_div=100
f_av =[0,0,0,0,0]
var fp=0, av, nn
var ttd 
var lang

gI = function () {}
gI.id = function (div) { return document.getElementById(div);}
gN = function () {}
gN.n = function (div) { return document.getElementsByName(div)[0];}

function rdt(){
tg=new XMLHttpRequest()
tg.open("GET","txt/text_"+parent.Lang+'.txt',false)
tg.send("")
ttd = tg.responseText.split("\r\n")
}

function lg(){
l=new XMLHttpRequest()
l.open("GET","txt/lang_"+parent.Lang+'.txt',false)
l.send("")
lang = l.responseText.split("\r\n")
if(parent.Lang==0){ tt=","; ts="." }
if(parent.Lang==1){ tt="."; ts="," }

}

function slg(){
var nodes= document.body.getElementsByTagName('*'),
L=nodes.length,temp;
while(L){
temp= nodes[--L].id || ''
if(temp.indexOf('lg_')== 0){
var nr=parseInt(temp.substring(3,6))
var tmp=gI.id(temp).innerHTML
gI.id(temp).innerHTML=lang[nr]+tmp}}
}

function slg_s(nr){
return lang[nr]
}

function dds(id){
gI.id(id).innerHTML="<span style='color:#990000'>"+slg_s(60)+"</span>"
gI.id("tb1").style.display="none"
gI.id("tb2").style.display="none"
gI.id("tb3").style.display="none"
}

function split_bin(i){
ret=new Array("","","","","","","","","","","","","","","","") 
var bin=parseInt(i).toString(2)
for(var j=0;j<bin.length;j++)
if(bin.substring(bin.length-1-j,bin.length-j)=="1") ret[j]="1"
return ret
}

function ok_fail_res() {
gI.id("ok").style.display="none"
gI.id("fail").style.display="none"
}

function sc(t) {
var f=document.forms[0];
for(var i=0;i<f.elements.length;i++){
if(f.elements[i].getAttribute('onchange')==null)
{f.elements[i].onchange=function(){gN.n(t).style.color='#CC0066'}}}
}

function nf( x, len, sep, ts) {
x = Math.round( x * Math.pow(10, len) ) / Math.pow(10, len);
str_no = x+"";
arr_int = str_no.split(".");
if(!arr_int[0]) arr_int[0] = "0";
if(!arr_int[1]) arr_int[1] = "";
if(arr_int[1].length < len){
nk = arr_int[1];
for(i=arr_int[1].length+1; i <= len; i++){  nk += "0";  }    arr_int[1] = nk;}
if(ts != "" && arr_int[0].length > 3){
b = arr_int[0];
arr_int[0] = "";
for(j = 3; j < b.length ; j+=3){
Extrakt = b.slice(b.length - j, b.length - j + 3);
arr_int[0] = ts + Extrakt +  arr_int[0] + "";}
str_first = b.substr(0, (b.length % 3 == 0)?3:(b.length % 3));
arr_int[0] = str_first + arr_int[0];}
return arr_int[0]+sep+arr_int[1];
}

function p_tm(t,typ){
if(!typ)	//hh:mm
	tim=aLZ(t.getHours(),2)+":"+aLZ(t.getMinutes(),2)	
if(typ==1)	//hh:mm:ss
	tim=aLZ(t.getHours(),2)+":"+aLZ(t.getMinutes(),2)+":"+aLZ(t.getSeconds(),2)	
if(typ==2)	//tt.mm.jj, hh:mm:ss
	tim=aLZ(t.getDate(),2)+"."+aLZ(t.getMonth()+1,2)+"."+(t.getYear()-100)+", "+aLZ(t.getHours(),2)+":"+aLZ(t.getMinutes(),2)+":"+aLZ(t.getSeconds(),2)	
if(typ==3)	//tt_mm_jj_hh_mm_ss
	tim=aLZ(t.getDate(),2)+"-"+aLZ(t.getMonth()+1,2)+"-"+(t.getYear()-100)+"_"+aLZ(t.getHours(),2)+"-"+aLZ(t.getMinutes(),2)+"-"+aLZ(t.getSeconds(),2)	
if(typ==4)	//WT tt.mm.jj, hh:mm:ss
	tim=lang[t.getDay()+255]+" "+aLZ(t.getDate(),2)+"."+aLZ(t.getMonth()+1,2)+"."+(t.getYear()-100)+", "+aLZ(t.getHours(),2)+":"+aLZ(t.getMinutes(),2)+":"+aLZ(t.getSeconds(),2)	
return tim
}

function aLZ(n,l) {
var num = '' + n
while (num.length < l) num = '0' + num
return num
}

function repl(x){
var a=x.toString()
if(parent.Lang==0){
a=a.replace('.','')
a=a.replace(',','.')}
if(parent.Lang==1){
a=a.replace(',','')
a=a.replace('.','.')}
return a
}

function l_0(v){
var nz=16-v.length
if(nz) return Array(nz+1).join("0")+v 
return v
}

function B(b){
return Boolean(Number(b))
}

function init(){
xhttp=new XMLHttpRequest()
xhttp.open("GET","start.cfg",false)
xhttp.send("")
var p = xhttp.responseText.split(";") 

if(p[9]=='end'){
parent.Name=p[0]
newP.innerHTML=p[1]
parent.Name_Func=newP.innerHTML
parent.IP=p[2]
parent.Port=p[3]
var ll=p[4].split(",") 
parent.Login="<b>"+ll[0]+"</b> von "+ll[1]
var pp=p[5].split(",") 
parent.Typ=pp[0]
parent.PowMes=pp[1]
parent.Ver=p[6]
parent.alw=parseInt(p[7]).toString(2)}
parent.Lang=p[8]
}

function tm(){
xhttp=new XMLHttpRequest()
xhttp.open("GET","daten.cfg",false)
xhttp.send("")
var p = xhttp.responseText.split(";") 
if(p[10]=='end'){
parent.a_time=p[0]*1000
parent.Rel_chg=p[1]
parent.IO_chg=p[2]
parent.Log_chg=p[3]
parent.ClockStat=p[4]
parent.BootTime=parseInt(p[5])+parseInt(p[6])*65536
if(parent.Typ=='o')
parent.Temp=nf(parseFloat(p[7]/10),1,tt,ts)
else
parent.Temp=nf(parseFloat(p[7]),1,tt,ts)
parent.Log_o=p[8]
parent.Log_t=p[9]

parent.V=nf(parseFloat(p[11]/10),1,tt,ts)
parent.I=nf(parseFloat(p[12]/10000),4,tt,ts)
f_av[fp]=	parseFloat(p[13]/1000)
av=nn=0
for(var i=0; i<5;i++){av+=f_av[i];if(f_av[i])nn++}
if(++fp>4)fp=0
av=av/nn
parent.Hz=nf(av,3,tt,ts)
parent.W=nf(parseFloat(p[14]/100),2,tt,ts)
parent.VA=nf(parseFloat(p[15]/100),2,tt,ts)
parent.VAR=nf(parseFloat(p[16]/100),2,tt,ts)
parent.cosf=nf(parseFloat(p[17]/32768),4,tt,ts)
parent.Power_Event=p[18]

parent.Vd=parseFloat(p[11]/10).toFixed(1)
parent.Id=parseFloat(p[12]/100).toFixed(2)
parent.Hzd=parseFloat(p[13]/1000).toFixed(3)
parent.Wd=parseFloat(p[14]/100).toFixed(2)
parent.VAd=parseFloat(p[15]/100).toFixed(2)
parent.VARd=parseFloat(p[16]/100).toFixed(2)
parent.cosfd=parseFloat((p[17]/32768)*100).toFixed(2)
parent.tmp_t=p[19]
parent.rnd_t=p[20]}
}

function s(t,adr,auth){
xhttps=new XMLHttpRequest()
xhttps.open("POST",adr+"ctrl.htm"+auth,false)
xhttps.setRequestHeader("Content-type","text/plain")
if(t=='h') xhttps.send('h=0'); else xhttps.send(t)
}

function rel_r(adr,auth){
xhttpe=new XMLHttpRequest()
xhttpe.open("GET",adr+"strg.cfg"+auth,false)
xhttpe.send("")
a = xhttpe.responseText.split(";")
}

function io_r(adr,auth){
try{xhttpi=new XMLHttpRequest()
xhttpi.open("GET",adr+"ioa.cfg"+auth,false)
xhttpi.send("")
a = xhttpi.responseText.split(";")} catch(e){return false}
}

function r_read(adr,auth){
var b,i
rel_r(adr,auth)
if(ttd===undefined)rdt()
if(a[40]=='end'){
if(parent.Typ=='o') b=tb1; else b=tb	
for(i=0;i<8;i++){ if(!(i>0 && parent.Typ=='o')){
if(a[i*5+2]==1){b+="<td id=btn title='"+lang[25]+"' style='";if(a[i*5+1]==1) b+=bgs+bg_ea; else b+=bgs+bg_ee}
else {
var N=a[i*5+4]
var Nr=a[i*5+3];Nr++
b+="<td id=bt title='"+ttd[Nr]+"\n"
if(N.match('T') && N.match('K'))b+=lang[26]; else if(N.match('T'))b+=lang[27]; else if(N.match('K'))b+=lang[28]
if(a[i*5+2]==0) b+="' onclick=\"this.style.border='2px inset #ddd';this.style['box-shadow']='#ddd 0 0 0px';Rel_chg_a++;s('F"+i+"=S','"+adr+"','"+auth+"');del_rel()\""; else b+="'"
b+=" style='"
if(a[i*5+1]==1) b+=bge+bg_e; else b+=bga+bg_a}
b+=a[i*5]
if(i==3)b+="<tr height=30>"}}
b+="</table>" 
document.getElementById('rel_z').innerHTML=b}
}

function io_read(adr,auth){
var b,i;a=""
io_r(adr,auth)
if(a.length==1){
try{	
document.getElementById('io').style.visibility="collapse"}catch(e){}
return}
if(a[33]=='end'){
if(parent.Typ=='o') b=tb1; else b=tb	
for(i=0;i<8;i++){
if(!(i>0 && parent.Typ=='o')){
var s=B(a[i*4+2]),ea=B(a[i*4+3]),inv=B(a[i*4+4])
b+="<td title='"
if(!ea){
b+=lang[29]
if(inv){b+="\n"+lang[30];s=!s}
b+="' onClick=\"this.style.border='2px inset #ddd';this.style['box-shadow']='#ddd 0 0 0px';IO_chg_a++;s('E"+i+"=S','"+adr+"','"+auth+"');del_io()\""
b+=" id=bt style='"
if(s) b+=bge+bg_e; else b+=bga+bg_a}
else {
b+=lang[31]
if(inv){b+="\n"+lang[30];s=!s}
b+="' id=bte style='"
if(s) b+=bgs+bg_ee; else b+=bgs+bg_ea}
b+=a[i*4+1] 
if(i==3)b+="<tr height=30>"}}
b+="</table>" 
document.getElementById('io_z').innerHTML=b}
}

function del_rel(){window.setTimeout("r_read('','')",Delay)}
function del_io(){window.setTimeout("io_read('','')",Delay)}
function ref(){rel_ch();io_ch()}
function rel_ch(){if(parent.Rel_chg!=Rel_chg_a){Rel_chg_a=parent.Rel_chg;r_read('','')}}
function io_ch(){if(parent.IO_chg!=IO_chg_a){IO_chg_a=parent.IO_chg;io_read('','')}}
